﻿#-*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Name:        遍历
# Purpose:
# Author:      SUMMVING
# Email:       summving@gmail.com
# Created:     2013/08/13  22:02
# Copyright:   (c) SUMMVING 2013
# Licence:     GPLv3
#-------------------------------------------------------------------------------

import time                     #引用一个时间模块，后面time.sleep()延时函数会用到

"""直接取值"""
for x in "hello":               #遍历序列，依次输出每个字符，h e l l o
    print x
else:
     print "ending"             #当出现其他情况（循环结束时）输出ending
print '----'

"""以索引取值"""
s = "world"
for y in range(len(s)):         #用len()函数确定字符串s的长度，得出索引
    print s[y]                  #用s的索引来取值，每行依次输出一个字符

print "----"

"""字符串""，元组()，列表[]，字典{}的遍历"""
str = "hello"                    #字符串""
tup = (1,2,3,'a','b','c')        #元组()
lis = [4,5,6,'d','e','f']        #列表[]
dic = {7:100,8:110,9:111}        #字典{}

for g in dic:
    print 'g =',g                #输出dic的元素
    print 'dic[g] =',dic[g]      #输出dic元素的值
"""
g = 8
dic[g] = 110
g = 9
dic[g] = 111
g = 7
dic[g] = 100
"""
print '----'


print 'dic.item() =',dic.items() #输出字典dic的元素及值
                                 #dic.item() = [(8, 110), (9, 111), (7, 100)]
print '----'

for h,i in dic.items():          #将字典里的元素对应于h，元素值对应于i
    print 'h =',h
    print 'i =',i
print '----'

a1,a2,a3,a4,a5,a6 = tup          #将tup里的元数赋依次值到a1~a6
print 'a1 =',a1,'a5 =',a5

print '----'

"""设置打印延时"""               #开头引用了time模块
for v in dic:
    print v
    print dic[v]
    time.sleep(1)                #每次打印完停顿1秒，dic有3个元素将停顿3秒
print '----'

"""for嵌套if"""
for x in range(1,11):            #设置x的取值范围为1到10，range函数必须用()
    print x
    if x == 5:                   #如果x等于5时中断输出
       break
else:                            #否则输出ending
     print 'ending'





